Imports System.ComponentModel
Imports System.Web.UI

<DefaultEvent("ConnectionFailed"), _
 InitializationEvent("Startup"), _
 ToolboxData("<{0}:Delegates runat=server></{0}:Delegates>")> _
 Public Class Delegates
    Inherits System.Web.UI.WebControls.WebControl
    Implements IPostBackDataHandler
    Implements IPostBackEventHandler

    Delegate Function Sample1(ByVal FirstParameter As String, _
                                   ByVal SecondParameter As Integer) As Boolean
    Delegate Sub Sample2()

    Public Delegate Sub EventDelegate(ByVal sender As Object, ByVal e As EventArgs)
    Public Event MyEvent As EventDelegate
    Public Event fred As EventHandler
    Public Event Changed As EventHandler

    Public Delegate Sub ConnectionFailedEventHandler(ByVal sender As Object, _
                           ByVal e As ConnectionFailedEventArgs)

    <Description("A test event"), Category("Action")> _
    Public Event ConnectionFailed As ConnectionFailedEventHandler

    Delegate Sub StartupEventHandler(ByVal sender As Object, ByVal e As EventArgs)
    Public Event Startup As StartupEventHandler

    Dim bolDirty As Boolean
    Public Sub RaisePostDataChangedEvent() Implements IPostBackDataHandler.RaisePostDataChangedEvent
        RaiseEvent Changed(Me, EventArgs.Empty)
    End Sub

    Public Sub RaisePostBackEvent(ByVal eventArgument As String) Implements IPostBackEventHandler.RaisePostBackEvent
        If eventArgument <> "AutoPostBk" Then
            RaiseEvent fred(Me, EventArgs.Empty)
        End If
    End Sub

    Public Function LoadPostData(ByVal postDataKey As String, ByVal postCollection As Collections.Specialized.NameValueCollection) As Boolean Implements IPostBackDataHandler.LoadPostData
        If postCollection(Me.UniqueID) <> Me.ViewState(Me.UniqueID) Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Sub Test()
        Dim del1 As New Sample1(AddressOf CalledRoutine1)
        Dim del2 As New Sample2(AddressOf CalledRoutine2)
        del1.Invoke("Peter", 55)
        del2.Invoke()

        Try
            '...connection
        Catch ex As Exception
            Dim e As New EventArgs
            RaiseEvent MyEvent(Me, e)

            Dim e2 As New ConnectionFailedEventArgs

            e2.ErrorCode = 200

            RaiseEvent ConnectionFailed(Me, e2)
            OnConnectionFailed(e2)
            OnStartup(e)
        End Try

    End Sub

    Protected Overrides Sub CreateChildControls()
        Dim txt As New WebControls.TextBox
        Dim txt2 As New WebControls.TextBox
        Dim strPostBackCode As String
        strPostBackCode = Me.Page.ClientScript.GetPostBackEventReference(Me, "AutoPostBk")

        Me.Controls.Add(txt)
        Me.Controls.Add(txt2)
        txt.Attributes("onblur") = strPostBackCode

        Dim btn As New System.Web.UI.WebControls.Button
        btn.Text = "Click Me"
        btn.ID = "btnSubmit"
        btn.CommandName = btn.ID
        btn.CommandArgument = "Click"

        Dim ev As EventHandler
        ev = New EventHandler(AddressOf Me.PassAnEvent)
        AddHandler btn.Click, ev
        Me.Controls.Add(btn)

    End Sub

    Sub PassAnEvent(ByVal sender As Object, ByVal e As EventArgs)
        Me.RaiseBubbleEvent(sender, e)
    End Sub

    Protected Overrides Function OnBubbleEvent(ByVal source As Object, _
          ByVal args As System.EventArgs) As Boolean

        Dim cbtn As WebControls.Button
        Dim cent As WebControls.CommandEventArgs

        Select Case (source.GetType.Name)
            Case "WebControl.Button"
                cbtn = CType(source, WebControls.Button)
                Select Case cbtn.CommandName
                    Case "btnSubmit"
                        cent = CType(args, WebControls.CommandEventArgs)
                        If cent.CommandArgument = "Click" Then
                            Me.dirty = True
                        Else
                            Me.dirty = False
                        End If
                        '...code for other buttons
                End Select
            Case "WebControl.TextBox"
                '...etc. ...
        End Select

    End Function

    Sub OnConnectionFailed(ByVal e As ConnectionFailedEventArgs)
        RaiseEvent ConnectionFailed(Me, e)
        Select Case e.Command
            Case "Retry"
                '...code to handle connection failure
        End Select
    End Sub

    Sub OnStartup(ByVal e As EventArgs)
        RaiseEvent Startup(Me, e)
    End Sub

    Function CalledRoutine1(ByVal FirstParameter As String, _
                                   ByVal SecondParameter As Integer) As Boolean

    End Function

    Sub CalledRoutine2()
        Me.HasEvents()
    End Sub

    Protected Overrides Sub render(ByVal writer As System.Web.UI.HtmlTextWriter)
        MyBase.Render(writer)
        writer.Write("Render the Delegates control")
    End Sub

    <IDReferenceProperty(GetType(Customer))> _
    Public Property CustomerId() As String
        Get
            Dim cust As New Customer
            Return cust.Id
        End Get
        Set(ByVal value As String)

        End Set
    End Property

    Sub GetCustomerProperties()
        Dim atts As System.ComponentModel.AttributeCollection
        Dim rf As IDReferencePropertyAttribute
        Dim typ As Type

        atts = TypeDescriptor.GetAttributes(Me)
        rf = atts(GetType(IDReferencePropertyAttribute))
        typ = rf.ReferencedControlType

    End Sub

    Public Property Sam()
        Get
            Return "x"
        End Get
        Set(ByVal value)

        End Set
    End Property
    Private Sub Delegates_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Page.RegisterRequiresPostBack(Me)
    End Sub

    Private Sub Delegates_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Public Property dirty() As Boolean
        Get
            Return bolDirty
        End Get
        Set(ByVal value As Boolean)
            bolDirty = value

        End Set
    End Property

    Protected Overrides Sub AddedControl(ByVal control As System.Web.UI.Control, _
                                           ByVal index As Integer)
        If control.GetType.Name = "TextBox" Then
            control.ID = "NewText" & index.ToString
        End If
    End Sub

End Class
